package org.example.pingkunsheng.controller;

import org.example.base.common.model.TRequestLogModel;
import org.example.base.common.model.UserModel;
import org.example.base.common.service.TRequestLogService;
import org.example.core.controller.BaseController;
import org.example.core.enums.ApiResult;
import org.example.core.mysql.SearchPageResult;
import org.example.core.mysql.Where;
import org.example.pingkunsheng.model.TTuitionWaiverModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Description: 日志记录
 * Date: 2024/2/5 23:37
 */
@RestController
@RequestMapping("/request/")
public class TRequestLogController extends BaseController<String, TRequestLogService, TRequestLogModel> {

    @Autowired
    private TRequestLogService tRequestLogService;

    @Override
    public ApiResult searchPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam(required = false) Map<String, Object> params) {
        SearchPageResult<TRequestLogModel> result = new SearchPageResult<>();
        Object ip = params.get("ip");
        Where where  = Where.build().field("1").equal(1);
        if (!ObjectUtils.isEmpty(ip)){
            where.and().field("ip").equal(ip);
        }
        int totalCount = tRequestLogService.count(where);
        List<TRequestLogModel> resultList = tRequestLogService.search(where.limit(page, limit));
        result.setPage(page);
        result.setLimit(limit);
        result.setTotalCount(totalCount);
        result.setList(resultList);
        return ApiResult.success(result);
    }
}
